#ifndef __DSTH_CONNECTOR_H__
#define __DSTH_CONNECTOR_H__

#include "Socket.h"
#include "Channel.h"

namespace dsth {
class EventLoop;
class Connector {
public:
    typedef Delegate<void (int)> ConnectHandler;
    typedef Delegate<void ()> ConnectRefusedHandler;
    Connector(EventLoop* loop, const InetSocketAddress& svc_addr);
    ~Connector();
    void Connect();
    void SetConnectHandler(const ConnectHandler& handler) { conn_handler_ = handler; }
    void SetConnectRefusedHandler(const ConnectRefusedHandler& handler) { refused_handler_ = handler; }
private:
    // Can't invoke copy constructor or assignment operator
    Connector(const Connector&);
    void operator=(const Connector&);

    void OnWriteEnable();
    void EstableishConnection();
    void DestroyChannel();
    
    EventLoop* loop_;
    Socket conn_socket_;
    Channel channel_;
    InetSocketAddress svc_addr_;

    ConnectHandler conn_handler_;
    ConnectRefusedHandler refused_handler_;
};

} // namespace
#endif // __DSTH_CONNECTOR_H__

